<?php
	defined('_JEXEC') or die('Restricted access');
    // rquire
	require_once("strings_utility_namespace.php");
    require_once(SOURCE_FOLDER.DIRECTORY_SEPARATOR."php_components".DIRECTORY_SEPARATOR."base_components".DIRECTORY_SEPARATOR."log_class.php");
    //
	//t_url_parameter
	//
	class t_url_parameter
	{
		// const declaration
		// public
		// D
    	const default_method_label = 'default';

		// property declaration
		// public
		// A
		public $argument = false;
        const ampersand_format = '&';
        const argument_format = 'a=';
		// C
		public $component_id = false;
        const component_format = 'c=';
        
		// U
		// The level is calculated counting the occurence of the character self::separator_level_character
		// The position of the layer is represented by the number extract between characters
		const url_position_of_value_level_0 = 0;  //position level 0 es: @a@b@c@
		const url_position_of_value_level_1 = 1;  // position level 0_position level 1 es: @|a|a1|an|@|b|b1|b2|@|c|c1|c2|@		// M
		// H
        const hide_ampersand = '___hide_ampersand___';
		const hide_argument = '___hide_argument___';
        const hide_component = '___hide_component___';
        const hide_method = '___hide_method___';
        const hide_separator_level_0 = '___hide_level_0___';
		const hide_separator_level_1 = '___hide_level_1___';
        
		// M
		public $method = false;
        const method_format = 'm=';
        // R
        public $request_type = false;
        const request_type_new_page = 'n';
        const request_type_no_page = 'o';
		const request_type_no_page_no_session = 's';
		// S
		const separator_level_0 = '@';
		const separator_level_1 = '|';
		const separator_level_character = '_';
		
		// method declaration
		public function __construct($a_component_id = null,$a_method = null,
			$a_argument = null,$a_request_type = t_url_parameter::request_type_new_page) 
		{
            
			$this->component_id = $a_component_id;
			$this->method = $a_method;
            //$search  = array("'");
            //$replace = array('%27');
			//$this->argument = str_replace($search, $replace, $a_argument);//rawurlencode($a_argument);
            $this->argument = $a_argument;
			$this->request_type = $a_request_type;
			//echo "this->argument :$this->argument <br>";
            if (! $this->is_a_correct_request_type())
            {
                $this->request_type = self::request_type_new_page;
            }
            //echo "this->request_type:$this->request_type";
		}
        
		// A
        public function append_to_argument_value ($a_position,$a_value)
        {
            //echo __METHOD__. " - TO TEST<BR>";
            
            //
			//echo "local_parameter_position: '$local_parameter_position'";
            
            $local_position_array = explode(self::separator_level_character,
				$a_position);
            $local_level = count($local_position_array)-1;
            //echo "local_level: ".$local_level; 
			if ($local_level = self::url_position_of_value_level_0)
			{
                //$this->argument .= $a_value.self::separator_level_0;
                $local_value_array_level_0 = explode(self::separator_level_0,$this->argument);
                $local_value_array_level_0[$local_position_array[0]] .= $a_value;
                $this->argument = implode(self::separator_level_0, $local_value_array_level_0);
			}
			if ($local_level = self::url_position_of_value_level_1)
			{
                $local_value_array_level_0 = explode(self::separator_level_0,$this->argument);
                $local_value_array_level_1 = explode(self::separator_level_1,$local_value_array_level_0[$local_position_array[0]]);
                $local_value_array_level_1[$local_position_array[1]] .= $a_value;
                $local_value_array_level_0[$local_position_array[0]] = implode(self::separator_level_1, $local_value_array_level_1);
                $this->argument = implode(self::separator_level_0, $local_value_array_level_0);
                
			}

        } 
        
        // G
        public function get_arguments_count ()
        {
			$local_level_0_array = explode(self::separator_level_0,$this->argument);
			return count($local_level_0_array)-1;
        }
		public function get_values_hidden_arguments ()
        {
			global $g_ambient_namespace;
    
			return $g_ambient_namespace->hide_separator($this->argument);
        }        
		
        public function get_argument_value ($a_position)
        {
            global $g_strings_utility_namespace;
            //
            $local_return = $this->argument;
			
            
            $local_position_array = explode(self::separator_level_character,
				$a_position);
			//print_r($local_position_array);	
            $local_level = count($local_position_array)-1;
			if ($local_level >= self::url_position_of_value_level_0)
			{
                if ($local_position_array[0] > -1)
					$local_return = $g_strings_utility_namespace->extract_from_position(
						$local_return,
						self::separator_level_0,
						$local_position_array[0]);
			}
			if ($local_level >= self::url_position_of_value_level_1)
			{
                $local_return = $g_strings_utility_namespace->extract_from_position(
					$local_return,
					self::separator_level_1,
					$local_position_array[1]);
			}
			//echo "local_return: ".$local_return; 
            return $local_return;

        }
        
        
        public function get_method_value ()
        {
			return $this->method;
        }
        //  I
        public function is_a_correct_request_type()
        {
            return (($this->request_type == self::request_type_no_page) || 
                ($this->request_type == self::request_type_new_page) ||
				($this->request_type == self::request_type_no_page_no_session));
        }
        public function is_a_no_default_request_type()
        {
            return ($this->request_type != self::request_type_new_page) ;
        }
        public function is_equal_to($a_parameter)
        {
            return (isset($a_parameter) && 
				($a_parameter->component_id == $this->component_id) &&
				($a_parameter->method == $this->method) &&
				($a_parameter->argument == $this->argument));
        }
        public function has_default_values()
        {
            return ((($this->method == self::default_method_label) || ($this->method == '')) && 
              ($this->argument == ''));
        }
        // S
        public function set_argument_value ($a_position,$a_value)
        {
            echo __METHOD__. " - TO TEST<BR>";
            
            //
			//echo "local_parameter_position: '$local_parameter_position'";
            
            $local_position_array = explode(self::separator_level_character,
				$a_position);
            $local_level = count($local_position_array)-1;
            //echo "local_level: ".$local_level; 
			if ($local_level = self::url_position_of_value_level_0)
			{
                //$this->argument .= $a_value.self::separator_level_0;
                $local_value_array_level_0 = explode(self::separator_level_0,$this->argument);
                if ($local_position_array[0] = -1)
                    $local_value_array_level_0[] = $a_value;
                else    
                    $local_value_array_level_0[$local_position_array[0]] = $a_value;
                $this->argument = implode(self::separator_level_0, $local_value_array_level_0);
			}
			if ($local_level = self::url_position_of_value_level_1)
			{
                $local_value_array_level_0 = explode(self::separator_level_0,$this->argument);
                $local_value_array_level_1 = explode(self::separator_level_1,$local_value_array_level_0[$local_position_array[0]]);
                if ($local_position_array[1] = -1)
                    $local_value_array_level_1[] = $a_value;
                else    
                    $local_value_array_level_1[$local_position_array[1]] = $a_value;
                $local_value_array_level_0[$local_position_array[0]] = implode(self::separator_level_1, $local_value_array_level_1);
                $this->argument = implode(self::separator_level_0, $local_value_array_level_0);
                
			}

        }        

	}
	//
	// t_url_parameters_list
	//
	class t_url_parameters_list
	{
		// contants
        const disguised_separator_level_0 = '/';
		const disguised_separator_level_1 = '-';
        // property 
        // public
		// C
		public $current_site = null;
		// F
		public $fast_parameter = '';
        //L
        public $log = false; //t_log        
		// P
        public $parameters_list = array();

		// method declaration
		public function __construct($a_import)
		{
			//print_r($this->parameters_list);
            //echo "a_import: $a_import<br>";
			
			//$this->import_parameters_from_http_predefined_variables();
            // creates $_GET,$_POST variable without amp;
			foreach($_GET as $variable => $value) 
            {
                $_GET[str_replace('amp;','',$variable)] = $value;
            }
            foreach($_POST as $variable => $value) 
            {
                $_POST[str_replace('amp;','',$variable)] = $value;
            }
            //
            if ($a_import == true)
			{
				$this->log = new t_log;
                $this->log->show_on_page = false;
                //
                $local_request_uri = urldecode($_SERVER['REQUEST_URI']);
                $local_query_string = urldecode($_SERVER['QUERY_STRING']);
                //
				$this->log->add_log_on_page("local_request_uri:".$local_request_uri);
                //echo "local_request_uri:".$local_request_uri."<br>";
				$this->log->add_log_on_page("local_query_string:".$local_query_string);
               // echo "local_query_string:".$local_query_string."<br>";
                //echo "count(_POST): ".count($_POST);
				
                if ($this->is_disguised_string())
				{
					//echo "import_parameters_from_disguised_string<br>";
                    $this->import_parameters_from_disguised_string($local_query_string);
				}
				else
				{
					//echo "import_parameters_from_http_predefined_variables<br>";
                    $this->import_parameters_from_http_predefined_variables();	
				}
				//
				if (!file_exists(SOURCE_FOLDER.DIRECTORY_SEPARATOR.'sites'.DIRECTORY_SEPARATOR.$this->current_site))
					$this->current_site = 'default';
				
			}
			else
			{
				global $g_ambient_namespace;
                $this->current_site = $g_ambient_namespace->current_site;
				$this->fast_parameter = $g_ambient_namespace->fast_parameter;
               // echo "g_ambient_namespace->fast_parameter: ". $g_ambient_namespace->fast_parameter."<br>";
                $this->log = $g_ambient_namespace->log;
				
			}
			//print_r($this->parameters_list);
		}
        //  A
        public function add_argument_value_to_str ($a_param_position,$a_str, $a_arg_count)
        {
			$local_parameter = $this->get_parameter_by_position($a_param_position); 
			$local_count = $a_arg_count;
			/*$local_arguments_count = 0;
			if (isset($local_parameter))
				$local_arguments_count = $local_parameter->get_arguments_count();
			*/
			$local_from = array();
			$local_to = array();
			$i = 0;
			
			while ($i <= $local_count)
			{
				array_push($local_from, "__CE_URL_PARAM_".$i."__");
				//$local_parameter->get_argument_value($i)
				if (isset($local_parameter))
					array_push($local_to, $local_parameter->get_argument_value($i));
				else	
				    array_push($local_to, '');
				$i++;
				//echo $i;
			}
			
			return str_replace($local_from,$local_to,$a_str);
		}
		
        public function add_new_parameter($a_component_id = null,$a_method = null,$a_argument = null,$a_request_type = t_url_parameter::request_type_new_page) 
		{   
            $local_result = new t_url_parameter($a_component_id,$a_method,$a_argument,$a_request_type);
            $this->parameters_list[] = $local_result;
            return $local_result;
        }
		//  G
		public function clear_parameters() 
		{
			$this->parameters_list = array();
            /*foreach ($this->parameters_list as $local_parameter_index)     
			{
				unset($this->parameters_list[$local_parameter_index]);
			}*/
		}		
       
        public function compute_disguised_parameters_string($a_skip_last_separator = false, $a_skip_current_site = false,$a_do_foundation_component_chek = true)
        {
			//echo "ciao";
			global $g_cuteengine;
			if (($a_skip_current_site) || ($this->current_site === "default"))
				$local_result = '';
			else
				$local_result = self::disguised_separator_level_0 . $this->current_site;
			//echo "1 - local_result:$local_result <br>";
           // echo "count: ". count($this->parameters_list). "<br>";
			foreach ($this->parameters_list as $local_parameter_index => $local_parameter) 
            {
				if (($a_do_foundation_component_chek) || (! $g_cuteengine->is_foundation_component($local_parameter->component_id)))
				{
					//echo "local_parameter_index: $local_parameter_index - local_parameter->component_id: $local_parameter->component_id";
					$local_result .= self::disguised_separator_level_0 . "c" . self::disguised_separator_level_1 .
						$local_parameter->component_id;
					//echo "2 - local_result:$local_result <br>";	
					//    
					//echo "local_parameter->request_type: $local_parameter->request_type <br>";
					if ($local_parameter->is_a_no_default_request_type() === true)    
						$local_result .= self::disguised_separator_level_1 . $local_parameter->request_type;
					//echo "3 - local_result:$local_result <br>";	
					//    argouments
					if (isset($local_parameter->argument) && ($local_parameter->argument != ''))
					{    
						$local_result .= self::disguised_separator_level_0 . 
							str_replace(t_url_parameter::separator_level_0, self::disguised_separator_level_1,$local_parameter->argument);
					}    
					//echo "4 - local_result:$local_result <br>";
					//    method
					//echo "local_parameter->method:".$local_parameter->method;
					if (isset($local_parameter->method) && ($local_parameter->method != '') &&
					   ($local_parameter->method != t_url_parameter::default_method_label))    
						$local_result .= self::disguised_separator_level_0 . $local_parameter->method;
					//echo "5 - local_result:$local_result <br>";	
				}	
            }
            //echo "local_result:$local_result";
            if ((!$a_skip_last_separator) and (strrchr($local_result, "/") != '/'))
                $local_result = $local_result . self::disguised_separator_level_0;
			if (($a_skip_last_separator) and (strrchr($local_result, "/") == '/'))
                $local_result = substr( $local_result , 0 ,-1);
			return $local_result;	
        }
		public function compute_http_predefined_variables($a_skip_exclamation_point = true,$a_skip_current_site = false,$a_do_foundation_component_chek = true)
		{
			global $g_cuteengine;
			if (($a_skip_current_site) || ($this->current_site === "default"))
				$local_result = '';
			else
				$local_result = "&s=" . $this->current_site;
		   
			foreach ($this->parameters_list as $local_parameter_index => $local_parameter) 
            {
                //echo "local_parameter_index: $local_parameter_index - local_parameter->component_id: $local_parameter->component_id<br>";
				if (($a_do_foundation_component_chek) || (! $g_cuteengine->is_foundation_component($local_parameter->component_id)))
				{
					/* 	local_parameter_index is used to avoid to overwriting the value of the same c,a,m  http predefined variables. 
						In this way we have c,c1,c2 a,a1,a2 m,m1,m2, etc  	*/
					//echo "local_parameter_index: $local_parameter_index - local_parameter->component_id: $local_parameter->component_id<br>";
                    if ($local_parameter_index == 0)
						$local_parameter_index_text = '';
					else	
						$local_parameter_index_text = "$local_parameter_index";
					//echo "local_parameter_index: $local_parameter_index;";
					$local_result .= "&c$local_parameter_index_text=" .
						$local_parameter->component_id;
					//echo "2 - local_result:$local_result <br>";	
					//    
					//echo "local_parameter->request_type: $local_parameter->request_type <br>";
					if ($local_parameter->is_a_no_default_request_type() === true)    
						$local_result .= "&r$local_parameter_index_text=".$local_parameter->request_type;
					//echo "3 - local_result:$local_result <br>";	
					//    argouments
					if (isset($local_parameter->argument))
					{    
						$local_result .= "&a$local_parameter_index_text=" . $local_parameter->argument;
					}    
					//echo "4 - local_result:$local_result <br>";
					//    method
					if (isset($local_parameter->method))    
						$local_result .= "&m$local_parameter_index_text=" . $local_parameter->method;
					//echo "5 - local_result:$local_result <br>";	
				}
            }
            if (!$a_skip_exclamation_point)
                $local_result = '?'.$local_result;
            return $local_result;
		}
		public function copy_from($a_source,$a_avoid_components_list) 
		{
			$this->clear_parameters();
			foreach ($a_source->parameters_list as $local_parameter_index => $local_parameter)     
			{
				if ($a_avoid_components_list[$local_parameter->component_id] == null)
					$this->parameters_list[$local_parameter_index] = $local_parameter;
			}
		}
		//  G
		public function get_parameter_by_component_id($a_component_id) 
		{
			$local_return = null;
			foreach ($this->parameters_list as $local_parameter_index => $local_parameter)     
			{
				if ($local_parameter->component_id == $a_component_id)
				{
					$local_return = $local_parameter;
					break;
				}
			}
            return $local_return;
			//var_dump( $this->parameters_list);
		}
		public function get_parameter_by_position($a_position) 
		{
			$local_return = null;
            if ((isset($a_position)) && ($a_position < count($this->parameters_list)))
            {
                $local_return = $this->parameters_list[$a_position];
            }    
            return $local_return;
			//var_dump( $this->parameters_list);
		}		
		// I
		public function import_parameters_from_http_predefined_variables()
		{
			//echo "import_parameters_from_http_predefined_variables";
			$local_component_index = 0;
            //echo 
			//print_r($_GET);

            if (array_key_exists('c',$_POST))
			{
               // echo '$_POST:';
              //  var_dump($_POST);
               // echo "<br>";
                $this->current_site = $_POST['s'];
                $local_component = $_POST['c'];
                $local_method = $_POST['m'];
                $local_argument = $_POST['a'];
                $local_request_type = $_POST['r'];
			}
			else
			{
               // echo '$_GET:';
               // var_dump($_GET);
               // echo "<br>";
                $this->current_site = $_GET['s'];
                $local_component = $_GET['c'];
                $local_method = $_GET['m'];
                $local_argument = $_GET['a'];
                $local_request_type = $_GET['r'];
			}
            
			if ((!isset($this->current_site)) || ($this->current_site == ''))
				$this->current_site = "default";
			
			while (isset($local_component))
            {
                $this->add_new_parameter($local_component,
					$local_method,$local_argument,$local_request_type);
				//
				$local_component_index ++;
				if (array_key_exists('c'.$local_component_index,$_POST))
				{
					$local_component = $_POST['c'.$local_component_index];
					$local_method = $_POST['m'.$local_component_index];
					$local_argument = $_POST['a'.$local_component_index];
					$local_request_type = $_POST['r'.$local_component_index];
				}
				else
				{
					$local_component = $_GET['c'.$local_component_index];
					$local_method = $_GET['m'.$local_component_index];
					$local_argument = $_GET['a'.$local_component_index];
					$local_request_type = $_GET['r'.$local_component_index];
				}

            }    
		}
		public function import_parameters_from_disguised_string($params_str)
		{
			$params_str_start = strlen(SITE_FOLDER);
            if ($params_str_start > 0)
            {
                $params_str_start .= $params_str_start + 1;
            }
            $params_str = substr($params_str,$local_start);
            //echo "params_str: $params_str";
            $params_str = preg_replace("/\/$/", "", $params_str);
            //echo "params_str:$params_str";
			$local_array_level_0 = explode(self::disguised_separator_level_0,$params_str);
			//
			$this->current_site = "default";
			$local_component_id = null;
			$local_request_type = null;
			$local_method = null;
			$local_argouments = null;
			//
			$local_current_param = 's';
			//$local_component_index = '';
			//
			$local_add_new = false;
			$local_count = count($local_array_level_0); 
            echo "local_count: $local_count<br>";
			foreach ($local_array_level_0 as $local_array_level_0_element_index => $local_array_level_0_element) 
			{	
                echo"local_array_level_0_element:$local_array_level_0_element - local_current_param:$local_current_param<br>"; 
				if ($local_array_level_0_element!= '')
				{
					$local_current_comp = 'c'/*.$local_component_index*/.self::disguised_separator_level_1;					
					
					//echo "local_array_level_0_element_index: $local_array_level_0_element_index - local_current_param: $local_current_param - ";
					echo "local_current_comp: $local_current_comp <br>";
					//
					if (substr($local_array_level_0_element, 0, strlen($local_current_comp)) === ($local_current_comp))
					{
						$local_component_id = null;
						$local_request_type = null;
						$local_method = null;
						$local_argouments = null;
						//
						$local_array_level_1 = explode(self::disguised_separator_level_1,$local_array_level_0_element);
						$local_component_id = $local_array_level_1[1];
						$local_request_type = $local_array_level_1[2];
						//echo "local_request_type: $local_request_type"; 
						//
						$local_current_param = 'a';
						//
						$local_next_comp = 'c'./*($local_component_index+1).*/self::disguised_separator_level_1;
						$this->log->add_log_on_page("local_count: $local_count - local_next_comp:$local_next_comp<br>");
						$local_add_new = ((($local_array_level_0_element_index+1) == $local_count) ||
							(substr($local_array_level_0[$local_array_level_0_element_index+1], 0, strlen($local_next_comp)) === ($local_next_comp)));
					}
					else if ($local_current_param == 's')
					{
						
                        //echo "file_exists(".SOURCE_FOLDER.DIRECTORY_SEPARATOR."sites".DIRECTORY_SEPARATOR.$local_array_level_0_element."): ".file_exists(SOURCE_FOLDER.DIRECTORY_SEPARATOR.'sites'.DIRECTORY_SEPARATOR.$local_array_level_0_element);
                        //$this->log->add_log_on_page("file_exists(".SOURCE_FOLDER.DIRECTORY_SEPARATOR."sites".DIRECTORY_SEPARATOR.$local_array_level_0_element."): ".file_exists(SOURCE_FOLDER.DIRECTORY_SEPARATOR.'sites'.DIRECTORY_SEPARATOR.$local_array_level_0_element));
                        if (file_exists(SOURCE_FOLDER.DIRECTORY_SEPARATOR.'sites'.DIRECTORY_SEPARATOR.$local_array_level_0_element))
						{
							$this->current_site = $local_array_level_0_element;
							
							$local_current_param = ('c'/*.$local_component_index*/.self::disguised_separator_level_1);
						}	
						else if ((strpos ($local_array_level_0_element, 'gclid') === false) && (strpos ($local_array_level_0_element, 'fb_xd_fragment') === false))
						{
							
							$this->fast_parameter = $local_array_level_0_element;
						}
					}
					else if ($local_current_param == 'a')
					{
						$local_argouments = str_replace(t_url_parameters_list::disguised_separator_level_1,t_url_parameter::separator_level_0,
							$local_array_level_0_element);
						//echo "local_argouments: $local_argouments <br>";
						$local_current_param = 'm';
						$local_next_comp = 'c'./*($local_component_index+1).*/self::disguised_separator_level_1;
						//echo "local_count: $local_count - local_next_comp:$local_next_comp<br>";
						$local_add_new = ((($local_array_level_0_element_index+1) == $local_count) ||
							(substr($local_array_level_0[$local_array_level_0_element_index+1], 0, strlen($local_next_comp)) === ($local_next_comp)));						
					}
					else if ($local_current_param == 'm')
					{
						$local_method = $local_array_level_0_element;
						//$local_current_param = '';
						//
						$local_add_new = true;
					}
					// Adds Param
					//echo "local_start_param:$local_start_param === local_current_param:$local_current_param";
					//echo "local_start_component_index:$local_start_component_index === local_component_index:$local_component_index";
					//echo "isset($local_component_id):".isset($local_component_id);
					if ($local_add_new === true)
					{
						echo "new_param - c:$local_component_id - m:$local_method - a:$local_argouments - r:$local_request_type<br>";
                        
						//$this->log->add_log_on_page("c:$local_component_id - m:$local_method - a:$local_argouments - r:$local_request_type");
						$this->add_new_parameter($local_component_id,$local_method,
							$local_argouments,$local_request_type);						
						//
						/*if ($local_component_index == '')
							$local_component_index = 1;
						else	
							$local_component_index ++;*/
						//	
						$local_current_param = ('c'/*.$local_component_index*/.self::disguised_separator_level_1);	
						//
						$local_add_new = false;
					}	
					
				}
			}
			//echo "import_parameters_from_disguised_string($params_str)";
			/*$local_params_str =  str_replace("/", "&", $params_str);
			$local_params_str =  str_replace("-", "=", $local_params_str);
			parse_str($local_params_str);
			$local_var_comp = "c";
			$local_var_method = "m";
			$local_var_arg = "a";
			
			while (isset(${$local_var_comp}))
            {
				$this->parameters_list[] = new t_url_parameter(${$local_var_comp},${$local_var_method},${$local_var_arg});
				//
				$local_component_index ++;
				$local_var_comp = "c".$local_component_index;
				$local_var_method = "m".$local_component_index;
				$local_var_arg = "a".$local_component_index;
				
			}*/
            
		}
		
		public function is_disguised_string() 
		{
            return ((! array_key_exists('c',$_POST)) && (! array_key_exists('c',$_GET)));
            
        }
		public function is_empty() 
		{
            return (count($this->parameters_list) == 0);
            
        }
        
	}
	global $g_url_parameters_namespace;
    $g_url_parameters_namespace = new t_url_parameters_list(true);
?>